package cc.smarnet.caihou.phone.deliver;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cc.smarnet.caihou.IPConfig;
import cc.smarnet.caihou.LoginActivity;
import cc.smarnet.caihou.R;
import cc.smarnet.caihou.adapter.DeliverAdapter;
import cc.smarnet.caihou.adapter.DeliverVarietyAdapter;
import cc.smarnet.caihou.adapter.SpacesItemDecoration;

import cc.smarnet.caihou.entity.Deliver;
import cc.smarnet.caihou.entity.DeliverVariety;
import cc.smarnet.caihou.phone.deliver.DeliverActivity;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 出库信息详情的Activity
 * */
public class DeliverDetailActivity extends AppCompatActivity {
    private ImageButton imageButton;
    private TextView orderNoTextView;//出库单号;
    private TextView numberTextView;//出库数量;
    private EditText stockOutUserEditText;//出库人;
    private EditText carDerverEditText;//司机;
    private EditText carNoEditText;//车牌;
    private EditText stockInTimeEditText;//出库时间;
    private TextView stateTextView;//状态;
    private EditText remarksEditText;//备注;
    private ProgressBar progressBar;
    private Button editButton;//出库信息编辑
    private Button saveButton;//出库信息保存
    private Button deleteButton;//删除
    private Button confirmButton;//确认出库
    private Button revokeButton;//撤销出库

    private Button varietyAddButton;//出库品种新增
    private RefreshLayout varietyRefreshLayout;
    private RecyclerView varietyRecyclerView;
    private LinearLayout varietyNoDataLinearLayout;//无品种数据
    private LinearLayout varietyFailLoadLinearLayout;//数据加载失败

    Deliver deliver;

    private String carDerver; // 司机
    private String carNo; // 车牌
    private String number; // 出库数量
    private String orderNo; // 订单号
    private int state; // 状态（-1待出库,0已出库，1删除，2撤销）
    private String stateString; // 状态
    private String stockInTime; // 出库时间
    private String stockOutUser; // 出库人
    private String remarks; // 备注

    private String token;
    
    private int pageNo = 1;// 取数分页的开始页
    private int pageSize = 4;// 每次取数的数量

    DeliverVarietyAdapter deliverVarietyAdapter;
    List<DeliverVariety> deliverVarietyList = new ArrayList<>();

    int mYear, mMonth, mDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deliver_detail);

        getSupportActionBar().hide();//隐藏标题栏

        initViews();

        varietyNoDataLinearLayout.setVisibility(View.GONE);// 隐藏暂无数据的信息
        varietyFailLoadLinearLayout.setVisibility(View.GONE);// 隐藏数据加载失败的信息

        //获取从DeliverActivity或DeliverVarietyAddActivity中传过来的值
        deliver = (Deliver) getIntent().getSerializableExtra("deliverDetail");

        state=deliver.getState();
        // 设置状态值
        switch (state){
            case -1:
                stateString="待出库";
                break;
            case 0:
                stateString="已出库";
                break;
            case 1:
                stateString="删除";
                break;
            case 2:
                stateString="撤销";
                break;
            default:
                stateString="未知";
        }
        stateTextView.setText(stateString);

        carDerver=deliver.getCarDerver();
        carNo=deliver.getCarNo();
        orderNo=deliver.getOrderNo();
        number=deliver.getNumber();
        stockInTime=deliver.getStockInTime();
        stockOutUser=deliver.getStockOutUser();
        remarks=deliver.getRemarks();

        carDerverEditText.setText(carDerver);
        carNoEditText.setText(carNo);
        orderNoTextView.setText(orderNo);
        numberTextView.setText(number);
        stockInTimeEditText.setText(stockInTime);
        stockOutUserEditText.setText(stockOutUser);
        remarksEditText.setText(remarks);
        setDisEnabled();
        getToken();

        progressBar.setVisibility(View.GONE);

        // 根据状态设置各按钮是否显示
        state=deliver.getState();
        setButtonVisibility(state);

        //获取品种信息
        getVarietyInfo();

        getDate();// 获取当前的日期
        // 点击出库日期的监听
        stockInTimeEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(DeliverDetailActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int day) {
                                mYear = year;
                                mMonth = month;
                                mDay = day;
                                String transMonth = String.format("%02d", mMonth + 1);// 如果月份小于10的话进行补0
                                String transDay = String.format("%02d", mDay);// 如果日小于10的话进行补0
                                stockInTime = mYear + "-" + transMonth + "-" + transDay;
                                stockInTimeEditText.setText(stockInTime);
                            }
                        }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        //点击出口信息编辑按钮的监听
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editButton.setVisibility(View.GONE);
                saveButton.setVisibility(View.VISIBLE);
                setEnabled();
            }
        });

        //点击出库信息保存按钮的监听
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 获取出库人
                if (stockOutUserEditText.getText().toString().equals("")) {
                    AlertDialog alertDialog = new AlertDialog.Builder(DeliverDetailActivity.this).create();
                    alertDialog.setMessage("请输入出库人！");
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    alertDialog.show();
                    return;
                } else {
                    stockOutUser = stockOutUserEditText.getText().toString();
                }

                // 保存入库信息
                save();
            }
        });

        //点击删除按钮的监听
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog alertDialog = new AlertDialog.Builder(DeliverDetailActivity.this).create();
                alertDialog.setMessage("是否确认删除");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        delete();
                    }
                });
                alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();
            }
        });

        //点击确认出库按钮的监听
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alertDialog = new AlertDialog.Builder(DeliverDetailActivity.this).create();
                alertDialog.setMessage("是否确认出库");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        confirmDeliver();
                    }
                });
                alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();
            }
        });

        //点击撤销出库按钮的监听
        revokeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alertDialog = new AlertDialog.Builder(DeliverDetailActivity.this).create();
                alertDialog.setMessage("是否确认撤销出库");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        revokeDeliver();
                    }
                });
                alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();
            }
        });

        // 点击返回按钮的监听
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DeliverDetailActivity.this, DeliverActivity.class);
                startActivity(intent);
            }
        });

        //点击出库品种按钮的监听
        varietyAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DeliverDetailActivity.this, DeliverVarietyAddActivity.class);
                intent.putExtra("deliverDetail", (Serializable) deliver);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                startActivity(intent);
            }
        });

        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局(参数是：上下文、列表方向（横向还是纵向）、是否倒叙)
        varietyRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        varietyRecyclerView.addItemDecoration(new SpacesItemDecoration(20));// 设置item的上下左右边距
        varietyRecyclerView.setHasFixedSize(true);
        // 这里使用适配器的目的在于使系统不再报‘E/RecyclerView: No adapter attached; skipping layout’的错误
        /*List<deliver> nullList=new ArrayList<>();
        deliverAdapter=new deliverAdapter(nullList);
        recyclerView.setAdapter(deliverAdapter);*/

        // 下拉刷新，目前在此就不做业务处理了，因为初始时加载的那几条数据就是从第一条开始的查询的
        varietyRefreshLayout.setOnRefreshListener(new OnRefreshListener() { //下拉刷新
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh();
            }
        });

        //上拉加载更多
        varietyRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(final RefreshLayout refreshlayout) {
                getVarietyInfo();
                refreshlayout.finishLoadMore(0);
            }
        });
    }

    public void initViews() {
        imageButton = findViewById(R.id.deliver_detail_return);
        numberTextView=findViewById(R.id.deliver_detail_number_textView);
        stockOutUserEditText=findViewById(R.id.deliver_detail_stockOutUser_editText);
        carDerverEditText=findViewById(R.id.deliver_detail_carDerver_editText);
        carNoEditText=findViewById(R.id.deliver_detail_carNo_editText);
        orderNoTextView=findViewById(R.id.deliver_detail_orderNo_textView);
        stockInTimeEditText=findViewById(R.id.deliver_detail_stockInTime_editText);
        stateTextView=findViewById(R.id.deliver_detail_state_editText);
        remarksEditText=findViewById(R.id.deliver_detail_remarks_editText);
        progressBar = findViewById(R.id.deliver_detail_progressBar);
        editButton = findViewById(R.id.deliver_detail_edit_button);
        saveButton = findViewById(R.id.deliver_detail_save_button);
        deleteButton = findViewById(R.id.deliver_detail_delete_button);
        confirmButton = findViewById(R.id.deliver_detail_confirm_button);
        revokeButton = findViewById(R.id.deliver_detail_revoke_button);

        varietyAddButton=findViewById(R.id.deliver_detail_variety_add_button);
        varietyRefreshLayout=findViewById(R.id.deliver_detail_variety_refreshLayout);
        varietyRecyclerView=findViewById(R.id.deliver_detail_variety_recyclerView);
        varietyNoDataLinearLayout=findViewById(R.id.deliver_detail_variety_noData_linearLayout);
        varietyFailLoadLinearLayout=findViewById(R.id.deliver_detail_variety_failLoad_linearLayout);
    }

    // 设置各控件不可用
    public void setDisEnabled() {
        carDerverEditText.setEnabled(false);
        carNoEditText.setEnabled(false);
        stockInTimeEditText.setEnabled(false);
        stockOutUserEditText.setEnabled(false);
        remarksEditText.setEnabled(false);
    }

    // 设置各控件可用
    public void setEnabled() {
        carDerverEditText.setEnabled(true);
        carNoEditText.setEnabled(true);
        stockInTimeEditText.setEnabled(true);
        stockOutUserEditText.setEnabled(true);
        remarksEditText.setEnabled(true);
    }

    /*
    * 根据状态设置各按钮是否显示
    * 如果状态为-1(待出库),那么删除按钮、出库信息编辑按钮、出库品种新增按钮、确认出库按钮显示，撤销出库按钮不显示，
    * 如果状态为0(已出库),那么删除按钮、出库信息编辑按钮、出库品种新增按钮、确认出库按钮不显示，撤销出库按钮显示，
    * 如果状态为1(删除),那么删除按钮、出库信息编辑按钮、出库品种新增按钮、确认出库按钮、撤销出库按钮都不显示，
    * */
    public void setButtonVisibility(int state){
        switch (state){
            case -1:
                deleteButton.setVisibility(View.VISIBLE);
                editButton.setVisibility(View.VISIBLE);
                varietyAddButton.setVisibility(View.VISIBLE);
                confirmButton.setVisibility(View.VISIBLE);
                revokeButton.setVisibility(View.GONE);
                break;
            case 0:
                deleteButton.setVisibility(View.GONE);
                editButton.setVisibility(View.GONE);
                varietyAddButton.setVisibility(View.GONE);
                confirmButton.setVisibility(View.GONE);
                revokeButton.setVisibility(View.VISIBLE);
                break;
            case 1:
                deleteButton.setVisibility(View.GONE);
                editButton.setVisibility(View.GONE);
                varietyAddButton.setVisibility(View.GONE);
                confirmButton.setVisibility(View.GONE);
                revokeButton.setVisibility(View.GONE);
                break;
            default:
                deleteButton.setVisibility(View.GONE);
                editButton.setVisibility(View.GONE);
                varietyAddButton.setVisibility(View.GONE);
                confirmButton.setVisibility(View.GONE);
                revokeButton.setVisibility(View.GONE);
        }
    }

    public void getToken() {
        SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        token = sp.getString("token", null);
    }

    // 获取当前日期
    public void getDate() {
        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
    }

    // 保存编辑的出库信息
    public void save() {
        progressBar.setVisibility(View.VISIBLE);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id",deliver.getId());
        map.put("carDerver",carDerver);//司机
        map.put("carNo",carNo);//司机
        map.put("stockInTime",stockInTime);//出库时间
        map.put("stockOutUser",stockOutUser);//出库人
        map.put("remarks",remarks);//备注
        String requestJsonString = JSON.toJSONString(map);

        String url = IPConfig.baseUrl + "/flower/flowerOutorder/edit";
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(mediaType, requestJsonString);
        OkHttpUtils
                .put()
                .addHeader("X-Access-Token", token)
                .url(url)
                .requestBody(body)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(DeliverDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(DeliverDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        progressBar.setVisibility(View.GONE);
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            AlertDialog alertDialog = new AlertDialog.Builder(DeliverDetailActivity.this).create();
                            alertDialog.setMessage("出库信息修改成功");
                            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(DeliverDetailActivity.this, DeliverActivity.class);
                                    startActivity(intent);
                                }
                            });
                            alertDialog.setCanceledOnTouchOutside(false);
                            alertDialog.show();
                        }
                    }
                });
    }

    // 删除出库信息
    public void delete() {
        progressBar.setVisibility(View.VISIBLE);
        String url = IPConfig.baseUrl + "/flower/flowerOutorder/fdelete/?id="+deliver.getId();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpUtils
                .delete()
                .addHeader("X-Access-Token", token)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(DeliverDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(DeliverDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        progressBar.setVisibility(View.GONE);
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            AlertDialog alertDialog = new AlertDialog.Builder(DeliverDetailActivity.this).create();
                            alertDialog.setMessage("出库信息删除成功");
                            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(DeliverDetailActivity.this, DeliverActivity.class);
                                    startActivity(intent);
                                }
                            });
                            alertDialog.setCanceledOnTouchOutside(false);
                            alertDialog.show();
                        }
                    }
                });
    }

    // 确认出库
    public void confirmDeliver(){
        progressBar.setVisibility(View.VISIBLE);
        String url = IPConfig.baseUrl + "/flower/flowerOutorder/sureOut";
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .addParams("id", deliver.getId())
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);// 设置加载效果不可见
                        Toast.makeText(DeliverDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(DeliverDetailActivity.this, LoginActivity.class);
                        startActivity(intent);                    }

                    @Override
                    public void onResponse(String response, int ID) {
                        progressBar.setVisibility(View.GONE);// 设置加载效果不可见
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        String message=JSON.parseObject(response).getString("message");
                        if (success) {
                            AlertDialog alertDialog = new AlertDialog.Builder(DeliverDetailActivity.this).create();
                            alertDialog.setMessage(message);
                            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(DeliverDetailActivity.this, DeliverActivity.class);
                                    startActivity(intent);
                                }
                            });
                            alertDialog.setCanceledOnTouchOutside(false);
                            alertDialog.show();
                        }else {
                            Toast.makeText(DeliverDetailActivity.this, message, Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }

    // 撤销出库
    public void revokeDeliver(){
        progressBar.setVisibility(View.VISIBLE);
        String url = IPConfig.baseUrl + "/flower/flowerOutorder/toWaitOut";
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .addParams("id", deliver.getId())
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);// 设置加载效果不可见
                        Toast.makeText(DeliverDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(DeliverDetailActivity.this, LoginActivity.class);
                        startActivity(intent);                    }

                    @Override
                    public void onResponse(String response, int ID) {
                        progressBar.setVisibility(View.GONE);// 设置加载效果不可见
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        String message=JSON.parseObject(response).getString("message");
                        if (success) {
                            AlertDialog alertDialog = new AlertDialog.Builder(DeliverDetailActivity.this).create();
                            alertDialog.setMessage("撤销出库成功！");
                            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(DeliverDetailActivity.this, DeliverActivity.class);
                                    startActivity(intent);
                                }
                            });
                            alertDialog.setCanceledOnTouchOutside(false);
                            alertDialog.show();
                        }else {
                            Toast.makeText(DeliverDetailActivity.this, message, Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }

    // 获取品种信息
    public void getVarietyInfo(){
        String url = IPConfig.baseUrl + "/flower/flowerOutorderinfo/list";
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .addParams("pageNo", String.valueOf(pageNo))
                .addParams("pageSize", String.valueOf(pageSize))
                .addParams("orderId", deliver.getId())
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);// 设置加载效果不可见
                        varietyFailLoadLinearLayout.setVisibility(View.VISIBLE);// 显示数据加载失败的信息
                    }

                    @Override
                    public void onResponse(String response, int ID) {
                        progressBar.setVisibility(View.GONE);// 设置加载效果不可见
                        varietyFailLoadLinearLayout.setVisibility(View.GONE);// 隐藏数据加载失败的信息
                        progressBar.setVisibility(View.GONE);// 设置加载效果不可见
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            String result = JSON.parseObject(response).getString("result");
                            String records = JSON.parseObject(result).getString("records");
                            JSONArray jsonArray = JSON.parseArray(records);
                            // 获取入库信息并存到deliverList中
                            for (int i = 0; i < jsonArray.size(); i++) {
                                String id = "";
                                String classes=""; // 等级
                                String flowerPoleLength=""; // 花杆长度
                                String number=""; // 数量(扎)
                                String packingSpecifications=""; // 规格
                                String variety="";//品种
                                String varietyCode=""; // 品种编码

                                JSONObject jsonObj = jsonArray.getJSONObject(i);
                                if (jsonObj.containsKey("id")) {
                                    id = jsonObj.getString("id");
                                }
                                if (jsonObj.containsKey("classes")) {
                                    classes = jsonObj.getString("classes");
                                }
                                if (jsonObj.containsKey("flowerPoleLength")) {
                                    flowerPoleLength = jsonObj.getString("flowerPoleLength");
                                }
                                if (jsonObj.containsKey("number")) {
                                    number = jsonObj.getString("number");
                                }
                                if (jsonObj.containsKey("packingSpecifications")) {
                                    packingSpecifications = jsonObj.getString("packingSpecifications");
                                }
                                if (jsonObj.containsKey("variety")) {
                                    variety = jsonObj.getString("variety");
                                }
                                if (jsonObj.containsKey("varietyCode")) {
                                    varietyCode = jsonObj.getString("varietyCode");
                                }

                                DeliverVariety deliverVariety = new DeliverVariety();
                                deliverVariety.setId(id);
                                deliverVariety.setClasses(classes);
                                deliverVariety.setFlowerPoleLength(flowerPoleLength);
                                deliverVariety.setNumber(number);
                                deliverVariety.setPackingSpecifications(packingSpecifications);
                                deliverVariety.setVariety(variety);
                                deliverVariety.setVarietyCode(varietyCode);

                                deliverVarietyList.add(deliverVariety);
                            }
                            if (deliverVarietyList.size() > 0) {// 如果deliverVarietyList中有数据的话就通过deliverAdapter将数据显示在界面上
                                deliverVarietyAdapter = new DeliverVarietyAdapter(getApplicationContext(),deliverVarietyList,deliver);
                                varietyRecyclerView.setAdapter(deliverVarietyAdapter);
                            }else {// 否则让暂无数据的信息显示在界面上
                                varietyNoDataLinearLayout.setVisibility(View.VISIBLE);
                            }

                            pageNo += 1;
                        }
                    }
                });
    }
}